package zh.dummy.netty.client.context;

import java.util.Set;

import io.netty.channel.Channel;
import zh.dummy.netty.NettyClient;
import zh.dummy.netty.client.context.listener.WorkflowListener;

public class NettyManager {
	
	private ListenerManager listenerManager;
	
	private NettyClient nettyClient;
	
	private String host;
	
	private int port;
	
	private volatile static NettyManager nettyManager;
	
	private volatile String id;
	
	private NettyManager(){
		listenerManager = new ListenerManager();
		nettyClient = NettyClient.getInstance();
		nettyManager = this;
	}
	
	public void startNettyClient(){
		nettyClient.bind(host,port);
		nettyClient.connect();
	}
	
	public void setPort(int port){
		this.port = port;
	}
	
	public void setHost(String host){
		this.host = host;
	}
	
	public static NettyManager getInstance(){
		if(nettyManager == null){
			synchronized (NettyManager.class) {
				nettyManager = new NettyManager();
			}
		}
		return nettyManager;
	}
	
	public void setWorkflowListeners(Set<WorkflowListener> listeners){
		listenerManager.addListeners(listeners);
	}
	
	public void setWorkflowListener(WorkflowListener listener){
		listenerManager.addListener(listener);
	}
	
	public Set<WorkflowListener> getListeners(){
		return listenerManager.getListeners();
	}


	public NettyClient getNettyClient() {
		return nettyClient;
	}

	public String getClientId() {
		return id;
	}

	public void setClientId(String id) {
		this.id = id;
	}

	public Channel getChannel() {
		return nettyClient.getChannel();
	}
	
}